<template>
  <div>
    <el-dialog
      title="新增权限点"
      :visible="dialogVisible"
      width="700px"
      @close="closeBtn"
    >
      <el-form ref="permissionForm" label-width="120px" :model="permissionForm" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="permissionForm.name" size="mini" />
        </el-form-item>

        <el-form-item label="权限标识" prop="code">
          <el-input v-model="permissionForm.code" size="mini" />
        </el-form-item>

        <el-form-item label="权限描述" prop="description">
          <el-input v-model="permissionForm.description" size="mini" />
        </el-form-item>

        <el-form-item label="开启">
          <el-switch
            v-model="permissionForm.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>

        <el-form-item>
          <el-row type="flex" justify="center" align="middle">
            <el-col :span="12">
              <el-button type="primary" size="mine" @click="submitForm">确定</el-button>
              <el-button size="mine" @click="closeBtn">取消</el-button>
            </el-col>

          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addPermission, edit } from '@/api/permission'
export default {
  name: 'PermissionDialog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    id: {
      type: Number,
      default: null
    },
    flag: {
      type: String,
      default: ''
    }

  },
  data() {
    return {
      permissionForm: {
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: '0',
        enVisible: '1'

      },
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    closeBtn() {
      this.$emit('update:dialogVisible', false)
      this.permissionForm = {
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: '0',
        enVisible: '1'

      }
      this.$emit('emptyId')
      this.$emit('emptyFlag')
      this.$refs.permissionForm.clearValidate()
    },
    async submitForm() {
      try {
        await this.$refs.permissionForm.validate()
        if (this.id) {
          if (this.flag === '编辑') {
            await edit(this.permissionForm)

            this.$message.success('修改权限点成功')
          } else {
            this.permissionForm.pid = this.id
            this.permissionForm.type = 2
            await addPermission(this.permissionForm)
            this.$emit('emptyId')
            this.$message.success('新增子级权限点成功')
          }
        } else {
          await addPermission(this.permissionForm)
          this.$message.success('新增权限点成功')
        }
        // 关闭dialog  重置表单
        // 通知父元素刷新页面
        this.$emit('updateForm')
        this.closeBtn()
      } catch (error) {
        console.log(error)
      }
    }
  }

}
</script>

<style>

</style>
